<template>
  <div class="login">
    <div class="loginForm">
        <h2>登录</h2>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="user">
            <el-input type="text" v-model="ruleForm.user"  autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass"  autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          </el-form-item>
        </el-form>
    </div>
  </div>
</template>

<script>
import memoryUtils from '../../utils/memoryUtils'
import storageUtils from '../../utils/storageUtils'
import { requestLogin } from '@/api'
export default {
  name: 'login',
  data () {
    const validateUser = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'));
      } else {
        callback();
      }
    };
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        user: '',
        checkPass: ''
      },
      rules: {
        user: [
          { validator: validateUser, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass, trigger: 'blur' }
        ]
      }
    };
  },
  created () {
    const user = memoryUtils.user;
    if (user && user.id) {
      this.$router.push('/');
    }
  },
  methods: {
    submitForm (formName) {
      const _this = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const {user: username, checkPass: password} = _this.ruleForm;
          /* 执行 axios的逻辑 */
          // ...
          if (username === 'admin' && password === 'admin') {
            requestLogin(username, password).then(res => {
              console.log(res);
            })

            const data = {
              id: 1, username: '王天一'
            }
            storageUtils.saveUser(data);
            memoryUtils.user = data;
            this.$router.push('/');
            this.$message.success('登录成功')
          } else {
            this.$message.error('登录失败')
            return false;
          }
        }
      });
    }
  }
}
</script>

<style  scoped>
  .login {
    /* 高度坍塌  */
    overflow: hidden;
    width: 100%;
    height: 100%;
    background-image: url('../../assets/bg.jpg');
    background-size: 100% 100%;
  }
  .loginForm {
    width: 400px;
    height: 300px;
    margin: 90px auto;
    padding: 30px 20px 0 0 ;
    background-color: #fff;
    border-radius: 20px;
    box-shadow: 0px 0px 10px #fff;
  }

  h2 {
    font-size: 20px;
    font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    font-weight: bold;
    text-align: center;
    line-height: 30px;
    height: 30px;
    margin-bottom: 15px;
  }
</style>
